<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-12
 * Time: 上午11:01
 */

namespace common\repositories\outstock;


use common\entities\outstock\MyscrmOutStockAttachmentEntity;
use common\repositories\BaseRepository;
use common\support\StringHelper;

class OutStockAttachmentRepository extends BaseRepository
{
    public function batchSave($orderId, $models)
    {
        foreach ($models as $model) {
            /** @var MyscrmOutStockAttachmentEntity $model */
            $model->id = StringHelper::uuid();
            $model->order_id = $orderId;
            if (!$model->validate() || !$model->save()) {
                $errors = $model->getFirstErrors();

                return self::setAndReturn(__LINE__, array_shift($errors));
            }
        }

        return true;
    }

    public function deleteByOrderId($orderId)
    {
        return MyscrmOutStockAttachmentEntity::deleteAll([
            'order_id' => $orderId,
        ]);
    }

    public function getAttachmentsByOrderId($orderId)
    {
        return MyscrmOutStockAttachmentEntity::find()->where(["order_id" => $orderId])->all();
    }
}